package com.bit.review;


import java.util.Comparator;
import java.util.Objects;

class Student implements Comparable<Student>{

    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
/**
 * 从逻辑上判断是否一样
 */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

   /* @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }*/

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        //return this.age-o.age;
        return this.name.compareTo(o.name);
    }
}

class AgeComparator implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return o1.age-o2.age;
    }
}
class NameComparator implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}
public class TestDemo {
    public static void main(String[] args) {
        Student student1 = new Student("bbc",19);
        Student student2 = new Student("bbc",19);
        System.out.println(student1.equals(student2));
    }

    public static void main2(String[] args) {
        Student student1 = new Student("ait",19);
        Student student2 = new Student("bbc",9);

        //AgeComparator ageComparator = new AgeComparator();
        NameComparator nameComparator = new NameComparator();
        //提出问题：  这两个对象要进行大小的比较  请问怎么比较？？？
        //if()括号里面一定要布尔类型
        if (nameComparator.compare(student1,student2)>0){
            System.out.println("student1>student2");
        }else {
            System.out.println("student1<=student2");
        }
        /*if (ageComparator.compare(student1,student2)>0){
            System.out.println("student1>student2");
        }else {
            System.out.println("student1<=student2");
        }*/
    }
    public static void main1(String[] args) {
        Student student1 = new Student("ait",19);
        Student student2 = new Student("bbc",9);

        //提出问题：  这两个对象要进行大小的比较  请问怎么比较？？？
        //if()括号里面一定要布尔类型
        if (student1.compareTo(student2)>0){
            System.out.println("student1>student2");
        }else {
            System.out.println("student1<=student2");
        }
    }
}
